package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.exception.DisplayableException;
import cn.wolfcode.p2p.base.service.IEmailService;
import cn.wolfcode.p2p.base.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class EmailController {

    @Autowired
    private IEmailService emailService;

    @RequestMapping("/sendEmail")
    @ResponseBody
    public AjaxResult emailPage(String email) {
        AjaxResult result = null;
        try {
            emailService.sendEmail(email);
            result = new AjaxResult("发送成功");
        } catch (DisplayableException e) {
            e.printStackTrace();
            //如果底层是抛出这个异常的话 则把 异常信息显示给用户看
            result = new AjaxResult(false, e.getMessage());
        } catch (Exception ex) {
            ex.printStackTrace();
            //把异常信息写入到日志中
            result = new AjaxResult(false, "系统异常,程序员哥哥正在努力抢救中...");
        }
        return result;
    }
}
